﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Datamining.Models;

namespace Datamining.Program
{
    public class ImportData
    {

        #region Read data from dataset

        /*
            Test data
            StreamReader reader = new StreamReader("../../Data/dataset_test.txt");
            Gebruik string[] parts = line.Split(',');
         */

        public void ReadTestData()
        {
            StreamReader reader = new StreamReader("../../Data/dataset_test.txt");

            Console.WriteLine("Beginnen met inlezen test rating data.");
            try
            {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    string[] parts = line.Split(',');
                    int userid = int.Parse(parts[0]);
                    int itemid = int.Parse(parts[1]);
                    double preferences = double.Parse(parts[2], System.Globalization.CultureInfo.InvariantCulture);

                    if (!Program.userRatingDictionary.ContainsKey(userid))
                    {
                        UserPreferences user = new UserPreferences();
                        user.UserId = userid;
                        user.setItemIDRating(itemid, preferences);
                        Program.userRatingDictionary.Add(user.UserId, user);
                    }
                    else
                    {
                        Program.userRatingDictionary[userid].setItemIDRating(itemid, preferences);
                    }

                    if (!Program.itemDictionary.ContainsKey(itemid))
                    {
                        Items item = new Items();
                        item.id = itemid;
                        item.itemName = "test";
                        item.linkIMDB = "geen";
                        item.date = "1-1-1970-test";
                        Program.itemDictionary.Add(item.id, item);
                    }
                }

                Console.WriteLine("Klaar met inlezen test rating data. \n");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

        // Voegt de Data uit de textfile toe aan de Dictionary
        /// <summary>
        /// Leest de dataset uit en zet de waardes in de SortedDictionary.
        /// </summary>
        public void ReadData()
        {
            StreamReader reader = new StreamReader("../../Data/udata.txt");

            Console.WriteLine("Beginnen met inlezen rating data.");
            try
            {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    string[] parts = line.Split('\t');
                    int userid = int.Parse(parts[0]);
                    int itemid = int.Parse(parts[1]);
                    double preferences = double.Parse(parts[2], System.Globalization.CultureInfo.InvariantCulture);

                    if (!Program.userRatingDictionary.ContainsKey(userid))
                    {
                        UserPreferences user = new UserPreferences();
                        user.UserId = userid;
                        user.setItemIDRating(itemid, preferences);
                        Program.userRatingDictionary.Add(user.UserId, user);
                    }
                    else
                    {
                        Program.userRatingDictionary[userid].setItemIDRating(itemid, preferences);
                    }
                }

                Console.WriteLine("Klaar met inlezen rating data. \n");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

        public void ReadGenre()
        {
            StreamReader reader = new StreamReader("../../Data/ugenre.txt");

            Console.WriteLine("Beginnen met inlezen genre data.");
            try
            {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    string[] parts = line.Split('|');
                    int genreId = int.Parse(parts[1]);
                    string genreName = parts[0];

                    Genres genre = new Genres();
                    genre.id = genreId;
                    genre.genreName = genreName;

                    Program.genreDictionary.Add(genre.id, genre);
                }

                Console.WriteLine("Klaar met inlezen genre data. \n");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

        public void ReadItem()
        {
            StreamReader reader = new StreamReader("../../Data/uitem.txt");

            Console.WriteLine("Beginnen met inlezen item data.");
            try
            {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    string[] parts = line.Split('|');
                    int itemId = int.Parse(parts[0]);
                    string name = parts[1];
                    string date = parts[2];
                    string linkIMDB = parts[4];

                    int genre1 = int.Parse(parts[5]);
                    int genre2 = int.Parse(parts[6]);
                    int genre3 = int.Parse(parts[7]);
                    int genre4 = int.Parse(parts[8]);
                    int genre5 = int.Parse(parts[9]);
                    int genre6 = int.Parse(parts[10]);
                    int genre7 = int.Parse(parts[11]);
                    int genre8 = int.Parse(parts[12]);
                    int genre9 = int.Parse(parts[13]);
                    int genre10 = int.Parse(parts[14]);
                    int genre11 = int.Parse(parts[15]);
                    int genre12 = int.Parse(parts[16]);
                    int genre13 = int.Parse(parts[17]);
                    int genre14 = int.Parse(parts[18]);
                    int genre15 = int.Parse(parts[19]);
                    int genre16 = int.Parse(parts[20]);
                    int genre17 = int.Parse(parts[21]);
                    int genre18 = int.Parse(parts[22]);
                    int genre19 = int.Parse(parts[23]);

                    Items item = new Items();
                    item.id = itemId;
                    item.itemName = name;
                    item.linkIMDB = linkIMDB;
                    item.date = date;
                    item.InsertInGenre(genre1);
                    item.InsertInGenre(genre2);
                    item.InsertInGenre(genre3);
                    item.InsertInGenre(genre4);
                    item.InsertInGenre(genre5);
                    item.InsertInGenre(genre6);
                    item.InsertInGenre(genre7);
                    item.InsertInGenre(genre8);
                    item.InsertInGenre(genre9);
                    item.InsertInGenre(genre10);
                    item.InsertInGenre(genre11);
                    item.InsertInGenre(genre12);
                    item.InsertInGenre(genre13);
                    item.InsertInGenre(genre14);
                    item.InsertInGenre(genre15);
                    item.InsertInGenre(genre16);
                    item.InsertInGenre(genre17);
                    item.InsertInGenre(genre18);
                    item.InsertInGenre(genre19);

                    Program.itemDictionary.Add(item.id, item);
                }

                Console.WriteLine("Klaar met inlezen item data. \n");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
        #endregion
    }
}
